import threading
import time

from crawer.bt_tiantang import BtTianTang


class ParserManager(threading.Thread):
    def __init__(self, manager):
        self.manager = manager
        self.bt_tian_tang = BtTianTang(self)
        threading.Thread.__init__(self)

    def run(self):
        print("start")
        while True:
            try:
                if not self.manager.parser_empty():
                    url_type, layer, content = self.manager.get_parser()
                    if url_type == 'bttiantang':
                        self.bt_tian_tang.parse(layer, content)
                else:
                    print("parser sleep")
                    time.sleep(1)
            except Exception as e:
                print("parser error")
                print(e)

    def add_url(self, item):
        self.manager.add_url(item)